package com.yanqu.road.logic.helper;

import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;

import java.util.ArrayList;
import java.util.List;

public class RankHelper {

    /**
     * 计算该次rankChange变化的排行
     *
     * @param oldRank 旧排行，-1或超出rankNum表示不在榜单内
     * @param newRank 新排行，-1或超出rankNum表示不在榜单内
     * @param maxRank 榜单长度
     * @return 需要变更的rank范围
     */
    public static List<Integer> calNeedDbRank(int oldRank, int newRank, int maxRank) {
        if (oldRank == -1 || oldRank > maxRank) oldRank = maxRank + 1;
        if (newRank == -1 || newRank > maxRank) newRank = maxRank + 1;
        int min = Math.min(oldRank, newRank);
        int max = Math.max(oldRank, newRank);
        List<Integer> list = new ArrayList<>();
        for (int i = min; i <= max; i++) {
            list.add(i);
        }
        return list;
    }

    /**
     * 榜单分页
     *
     * @param pageNo 从0开始
     */
    public static List<XSeasonUserRank> getPageRankList(List<XSeasonUserRank> allRankList, int maxRank, int pageNo, int pageSize) {

        int beginRank = Math.max(pageNo * pageSize + 1, 1);
        int endRank = Math.min(beginRank + pageSize - 1, maxRank);

        // 超出榜单了，不返回
        if (beginRank > allRankList.size()) {
            return new ArrayList<>();
        }

        List<XSeasonUserRank> resultList = new ArrayList<>();
        int rankNo = 0;
        for (XSeasonUserRank tmp : allRankList) {
            rankNo++;
            if (rankNo < beginRank) {
                continue;
            }
            if (rankNo > endRank) {
                break;
            }
            resultList.add(tmp);
        }
        return resultList;
    }

}
